package com.hjq.demo.ui.dialog

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bndg.browser.R
import com.hjq.base.BaseAdapter
import com.hjq.base.BaseDialog
import com.hjq.base.BottomSheetDialog
import com.hjq.base.action.AnimAction
import com.bndg.browser.app.AppAdapter
import java.util.ArrayList

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject-Kotlin
 *    time   : 2019/07/27
 *    desc   : 相册专辑选取对话框
 */
class BottomMediaDialog {

    class Builder(context: Context) : BaseDialog.Builder<Builder>(context),
        BaseAdapter.OnItemClickListener {

        private var listener: OnListener? = null
        private val recyclerView: RecyclerView? by lazy { findViewById(R.id.rv_media_list) }
        private val adapter: MediaAdapter

        init {
            setContentView(R.layout.media_dialog)
            setAnimStyle(AnimAction.ANIM_EMPTY)
            adapter = MediaAdapter(context)
            adapter.setOnItemClickListener(this)
            recyclerView?.adapter = adapter
            recyclerView?.setItemAnimator(null);
            adapter.setData(ArrayList())
        }

        fun setData(data: MutableList<MediaInfo>): Builder = apply {
            adapter.setData(data)
            // 滚动到选中的位置
        }

        fun setListener(listener: OnListener?): Builder = apply {
            this.listener = listener
        }

        override fun onItemClick(recyclerView: RecyclerView?, itemView: View?, position: Int) {
            // 延迟消失
            postDelayed({
                listener?.onSelected(getDialog(), position, adapter.getItem(position))
                dismiss()
            }, 300)
        }

        override fun createDialog(context: Context, themeId: Int): BaseDialog {
            val dialog = BottomSheetDialog(context, themeId)
            dialog.getBottomSheetBehavior().peekHeight =
                getResources().displayMetrics.heightPixels / 2
            return dialog
        }

        fun addData(videos: MutableList<MediaInfo>) {
            for (videoInfo in videos) {
                if (!adapter.containsItem(videoInfo)) {
                    adapter.addItem(videoInfo)
                }
            }
        }
    }

    class MediaAdapter constructor(context: Context) : AppAdapter<MediaInfo>(context) {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            return ViewHolder()
        }

        inner class ViewHolder : AppViewHolder(R.layout.media_item) {

            private val nameView: TextView? by lazy { findViewById(R.id.tv_media_name) }

            override fun onBindView(position: Int) {
                getItem(position).apply {
                    nameView?.text = if (title.isNullOrEmpty()) url else title
                }
            }
        }
    }

    data class MediaInfo(
        var url: String,
        var title: String? = null,
    ) {
    }

    interface OnListener {

        /**
         * 选择条目时回调
         */
        fun onSelected(dialog: BaseDialog?, position: Int, bean: MediaInfo)
    }
}